﻿using Brjnk.Common;
using Microsoft.VisualStudio.TestTools.UnitTesting;

namespace MapsTest
{
    [TestClass()]
    public class GPSCoordinatesTest
    {
        private TestContext testContextInstance;

        public TestContext TestContext
        {
            get
            {
                return testContextInstance;
            }
            set
            {
                testContextInstance = value;
            }
        }

        #region Additional test attributes

        //
        //You can use the following additional attributes as you write your tests:
        //
        //Use ClassInitialize to run code before running the first test in the class
        //[ClassInitialize()]
        //public static void MyClassInitialize(TestContext testContext)
        //{
        //}
        //
        //Use ClassCleanup to run code after all tests in a class have run
        //[ClassCleanup()]
        //public static void MyClassCleanup()
        //{
        //}
        //
        //Use TestInitialize to run code before running each test
        //[TestInitialize()]
        //public void MyTestInitialize()
        //{
        //}
        //
        //Use TestCleanup to run code after each test has run
        //[TestCleanup()]
        //public void MyTestCleanup()
        //{
        //}
        //

        #endregion Additional test attributes

        [TestMethod()]
        public void GPSCoordinatesConstructorTest()
        {
            double latitude1 = 49F;
            double longtitude1 = 15F;
            double latitude2 = 50F;
            double longtitude2 = 16F;
            GpsCoordinates target1 = new GpsCoordinates(latitude1, longtitude1);
            GpsCoordinates target2 = new GpsCoordinates(latitude2, longtitude2);

            var distance = target1 - target2;
            Assert.AreEqual(132.6, distance.GetDistanceValue(DistanceUnit.Kilometre), 0.2);
            Assert.AreEqual(132600.0, distance.GetDistanceValue(DistanceUnit.Metre), 200);
            var angle = new Azimuth(target1, target2);
            Assert.AreEqual(32.62, angle.ValueDeg, 0.1);
            angle = new Azimuth(target2, target1);
            Assert.AreEqual(213.38, angle.ValueDeg, 1);
        }

        [TestMethod()]
        [DeploymentItem("Maps.dll")]
        public void AreaTest()
        {
            GpsCoordinates NE = new GpsCoordinates(10, 10);
            GpsCoordinates NW = new GpsCoordinates(10, -10);
            GpsCoordinates SE = new GpsCoordinates(-10, 10);
            GpsCoordinates SW = new GpsCoordinates(-10, -10);
            Assert.AreEqual(CardinalDirection.NE, NE.Area);
            Assert.AreEqual(CardinalDirection.NW, NW.Area);
            Assert.AreEqual(CardinalDirection.SE, SE.Area);
            Assert.AreEqual(CardinalDirection.SW, SW.Area);
        }

        [TestMethod()]
        public void GpsTest2()
        {
            GpsCoordinates target1 = new GpsCoordinates(10.341666667, 20.51111111111);
            Assert.AreEqual(10.341666667, target1.LatTotalDegree, 0.00001);
            Assert.AreEqual(10, target1.LatDegree);
            Assert.AreEqual(20, target1.LatMinutes);
            Assert.AreEqual(20.5, target1.LatTotalMinutes, 0.0001);
            Assert.AreEqual(30, target1.LatTotalSeconds, 0.0001);

            Assert.AreEqual(20.5111111111, target1.LonTotalDegree, 0.00001);
            Assert.AreEqual(20, target1.LonDegree);
            Assert.AreEqual(30, target1.LonMinutes);
            Assert.AreEqual(30.666667, target1.LonTotalMinutes, 0.0001);
            Assert.AreEqual(40, target1.LonTotalSeconds, 0.0001);
        }

        [TestMethod()]
        public void GpsParseAndToString()
        {
            GpsCoordinates gps1, gps2, gps3, gps4, gps5;
            Assert.IsTrue(GpsCoordinates.TryParse("+50.35° -14.42°", out gps1));
            Assert.IsTrue(GpsCoordinates.TryParse("N51 13 44,3 E12 54 07.6", out gps2));
            Assert.IsTrue(GpsCoordinates.TryParse("  N    50° 30' ; W 15,325  ° ", out gps3));
            Assert.IsTrue(GpsCoordinates.TryParse("45° 32° 15'", out gps4));
            Assert.IsFalse(GpsCoordinates.TryParse("45°° 32° 15'", out gps5));
            Assert.IsFalse(GpsCoordinates.TryParse("", out gps5));
            Assert.IsFalse(GpsCoordinates.TryParse(" ", out gps5));
            Assert.IsFalse(GpsCoordinates.TryParse("X", out gps5));
            Assert.IsFalse(GpsCoordinates.TryParse("15° 32'", out gps5));
            Assert.AreEqual(gps1, new GpsCoordinates(50.35, -14.42));
            //Assert.AreEqual(gps2, new GpsCoordinates(51, 13, 44.3, 12, 54, 07.6));
            Assert.AreEqual(gps3, new GpsCoordinates(50.5, -15.325));
            //Assert.AreEqual(gps4, new GpsCoordinates(45, 0, 0, 32, 15, 0));

            Assert.AreEqual(gps1.ToString("m7s@@@", System.Globalization.CultureInfo.InvariantCulture), @"+50° 21.0000000'@@@-14° 25.2000000'");
        }
    }
}